package org.hackystat.ambientdevice.device;

import org.hackystat.ambientdevice.ConfigurationException;
import org.hackystat.ambientdevice.device.orb.AmbientOrb;
import org.hackystat.ambientdevice.device.bunny.Nabaztag;
import org.hackystat.ambientdevice.server.jaxb.Device;

/**
 *The device Factory that holds all the devices.
 * 
 * @author Shao xuan
 */
public class DeviceFactory {

  /** Enumeration class of devices. */
  enum DEVICE {
    AmbientOrb, Nabaztag, Unsupported;
    /**
     * Return a DEVICE enumeration corresponding to the string.
     * If no corresponding enumeration is found, Unsupported will be returned.
     * @param string the string
     * @return enumeration of DEVICE
     */
    public static DEVICE getEnumValue(String string) {
      try {
        return DEVICE.valueOf(string);
      }
      catch (IllegalArgumentException e) {
        return Unsupported;
      }
    }
  };

  /**
   * Gets the right device to use.
   * 
   * @param device the device to get
   * @throws ConfigurationException for errors
   * @return AmbientDevice to return
   */
  public AmbientDevice getDevice(Device device) throws ConfigurationException {
    if (device.getDeviceType() == null) {
      throw new ConfigurationException("Device type is null.");
    }
    switch (DEVICE.getEnumValue(device.getDeviceType())) {
      case AmbientOrb:
        return AmbientOrb.getOrb(device.getDeviceId());
      case Nabaztag:
        return Nabaztag.getBunny(device.getDeviceId(), device.getToken());
      default:
        throw new ConfigurationException("Device type " + device.getDeviceType()
          + " is not supported");
    }
  }
}
